package org.railage.client.beans;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.railage.client.extra.NewRouteRecord;
import org.railage.common.dto.StationOfRouteDTO;
import org.railage.common.protocol.Result;
import org.railage.server.services.RouteService;
import org.railage.server.services.StationService;

@ManagedBean(name = "editRoutes")
@ViewScoped
public class EditRoutesBean {

	private static final Logger log = Logger.getLogger(EditRoutesBean.class);

	private RouteService routeService = new RouteService();
	private StationService stationService = new StationService();

	private List<String> stations;
	private String[] routes;
	private List<StationOfRouteDTO> stationsOfRoute;
	private List<NewRouteRecord> newRouteRecords = new ArrayList<NewRouteRecord>();

	// delete action
	private String selectedRoute;

	// add action
	private String newRouteName;
	private String selectedStation;
	private Date timeAfterPrev;
	private Date timeOnStation;
	private int recordsCounter;

	private SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");

	public EditRoutesBean() {
		updateRoutesArr();
		updateStationsArr();
		recordsCounter = 0;

		try {
			timeAfterPrev = timeFormatter.parse("00:00");
			timeOnStation = timeFormatter.parse("00:00");
		} catch (ParseException e) {
			log.error("Time parse exception");
		}
	}

	public void updateRoutesArr() {
		routes = routeService.getRouteNames(null);
	}

	public void updateStationsArr() {
		stations = new LinkedList<String>(Arrays.asList(stationService
				.getStationNames(null)));
	}

	public void updateStationsOfRoute() {
		stationsOfRoute = Arrays.asList(stationService
				.getOrderedStationsOfRoute(selectedRoute));
	}

	public void delete() {
		Result result = routeService.deleteRoute(selectedRoute);
		updateRoutesArr();

		log.debug("Delete route: " + selectedRoute + "; Result: "
				+ result.getResult());

		FacesMessage message;
		if (result.isNoError()) {
			message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					result.getResult(), "");
			stationsOfRoute = null;
		} else {
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					result.getResult(), "");
		}
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void add() {
		log.debug("Add station on route: " + selectedStation);
		NewRouteRecord record = new NewRouteRecord();
		record.setNum(++recordsCounter);
		record.setStation(selectedStation);
		record.setTimeAfterPrev(timeFormatter.format(timeAfterPrev));
		record.setTimeOnStation(timeFormatter.format(timeOnStation));
		newRouteRecords.add(record);
		//stations.remove(selectedStation);
	}

	public void save() {
		String[] stationsArr = new String[newRouteRecords.size()];
		String[] timesAfterPrevArr = new String[newRouteRecords.size()];
		String[] timesOnStationsArr = new String[newRouteRecords.size()];

		int i = 0;
		for (NewRouteRecord record : newRouteRecords) {
			stationsArr[i] = record.getStation();
			timesAfterPrevArr[i] = record.getTimeAfterPrev();
			timesOnStationsArr[i] = record.getTimeOnStation();
			i++;
		}

		Result result = routeService.addRoute(new Object[] { newRouteName,
				stationsArr, timesAfterPrevArr, timesOnStationsArr });

		log.debug("Add route: " + newRouteName + "; Result: "
				+ result.getResult());

		FacesMessage message;
		if (result.isNoError()) {
			message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					result.getResult(), "");
			updateRoutesArr();
			updateStationsArr();
			recordsCounter = 0;
			newRouteRecords.clear();
		} else {
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					result.getResult(), "");
		}
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public List<NewRouteRecord> getNewRouteRecords() {
		return newRouteRecords;
	}

	public void setNewRouteRecords(List<NewRouteRecord> newRouteRecords) {
		this.newRouteRecords = newRouteRecords;
	}

	public String getSelectedRoute() {
		return selectedRoute;
	}

	public void setSelectedRoute(String selectedRoute) {
		this.selectedRoute = selectedRoute;
	}

	public String getNewRouteName() {
		return newRouteName;
	}

	public void setNewRouteName(String newRouteName) {
		this.newRouteName = newRouteName;
	}

	public String getSelectedStation() {
		return selectedStation;
	}

	public void setSelectedStation(String selectedStation) {
		this.selectedStation = selectedStation;
	}

	public Date getTimeAfterPrev() {
		return timeAfterPrev;
	}

	public void setTimeAfterPrev(Date timeAfterPrev) {
		this.timeAfterPrev = timeAfterPrev;
	}

	public Date getTimeOnStation() {
		return timeOnStation;
	}

	public void setTimeOnStation(Date timeOnStation) {
		this.timeOnStation = timeOnStation;
	}

	public List<String> getStations() {
		return stations;
	}

	public String[] getRoutes() {
		return routes;
	}

	public List<StationOfRouteDTO> getStationsOfRoute() {
		return stationsOfRoute;
	}

	public int getRecordsCounter() {
		return recordsCounter;
	}
	
	

}
